package com.kbook.action.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.kbook.bean.UserBean;
import com.kbook.dao.ChartsDao;
import com.kbook.dao.NovelDao;
import com.kbook.dao.impl.ChartsDaoImpl;
import com.kbook.dao.impl.NovelDaoImpl;
import com.kbook.log.LogWriter;
import com.kbook.tool.Constants;
import com.kbook.vo.Charts;
import com.kbook.vo.Novel;

public class SearchAction extends AbstractAction {

	private static final Logger logger = Logger.getLogger("SearchAction");
	
	@Override
	protected Object doAction(UserBean user, Map params) throws Exception {
		// TODO Auto-generated method stub
		Map resultMap = new HashMap();
		
		String keyword = (String)params.get("keyword");
		System.out.println(keyword);
		String type = (String)params.get("type");
		
		List<Novel> novelList = null;
		NovelDao novelDao = new NovelDaoImpl();
		
		if("1".equals(type)||StringUtils.isEmpty(type)){
			novelList = novelDao.queryNovelByName(keyword, 0, 24);
		}else{
			novelList = novelDao.queryNovelByAuthorName(keyword, 0, 24);
		}
		
		ChartsDao chartDao = new ChartsDaoImpl();
		List<Charts> hotestChartList = chartDao.queryById(Constants.CHART_HOTEST);
		List<Charts> newestChartList = chartDao.queryById(Constants.CHART_NEWEST);
		
		resultMap.put("keyword", keyword);
		resultMap.put("novelList", novelList);
		resultMap.put("totalNum", novelList.size());
		resultMap.put("hotestChartList", hotestChartList);
		resultMap.put("newestChartList", newestChartList);
		
		return resultMap;
	}

	@Override
	protected void afterAction(Object obj, UserBean user, Map params) {
		// TODO Auto-generated method stub
		List<String> logParams = new ArrayList<String>();
		logParams.add((String)params.get("ip"));
		logParams.add((String)params.get("ua"));
		logParams.add((String)params.get("cid"));
		logParams.add((String)params.get("keyword"));
		logParams.add((String)params.get("type"));

		LogWriter.getInstance().createLog(logParams, logger);
	}

	
}
